<?php


namespace app\admin\controller;


use app\admin\model\ProductCateModel;

use app\admin\model\ProductFileModel;

use think\Db;

class ProductFile extends Base
{
    //列表
    public function Index(){

        if(request()->isAjax ()){

            extract(input());

            $map = [];

            if(isset($key)&&$key!=""){

                $map['r.name'] = ['like',"%" . $key . "%"];

            }

            if(isset($start)&&$start!=""&&isset($end)&&$end=="")

            {

                $map['r.create_time'] = ['>= time',$start];

            }

            if(isset($end)&&$end!=""&&isset($start)&&$start=="")

            {

                $map['r.create_time'] = ['<= time',$end];

            }

            if(isset($start)&&$start!=""&&isset($end)&&$end!="")

            {

                $map['r.create_time'] = ['between time',[$start,$end]];

            }

            $field=input('field');//字段

            $order=input('order');//排序方式

            if($field && $order){

                $od="r.".$field." ".$order;

            }else{

                $od="r.sort";

            }

            $cate = new ProductFileModel();

            $nowpage = input('get.page') ? input('get.page'):1;

            $limits = input("limit")?input("limit"):10;

            $count = $cate->getAllCount($map);//计算总页面

            $lists = $cate->getAllCate($map, $nowpage, $limits,$od);

            for ($i = 0; $i < count($lists); $i++) {

                $photo = explode(',', $lists[$i]['img']);

                $lists[$i]['img'] = array_values($photo);

            }


            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);

        }

        $codepay_config['host'] = (isHTTPS() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'];

        $servername = $codepay_config['host'] . '/detail/';

        return $this->fetch("productfile/index", ['servername' => $servername]);
    }
    //添加
    public function add_obj()

    {

        if (request()->isPost()) {

            extract(input());

            $param = input('post.');

            if (!isset($status)) {

                $param['status'] = 1;

            }

            $param['img'] = trim($param['img'], ',');

            $obj = new ProductFileModel();

            $flag = $obj->insertObj($param);

            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);

        }

        $cate = new ProductCateModel();

        return $this->fetch('productfile/add', ['cate' => $cate->getCate(), 'configState' => config('qiniu_allow_state')]);

    }

    //编辑
    public function edit_obj(){

        $obj = new ProductFileModel();

        $cate = new ProductCateModel();

        if(request()->isPost()){

            $param = input('post.');

            $flag = $obj->editObj($param);

            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);

        }

        $id = input('param.id');

        $this->assign([

            'configState' => config('qiniu_allow_state'),

            'Obj'=>$obj->getOneObj($id),

            'cate' => $cate->getCate(),
        ]);

        return view('productFile/edit');
    }
    //删除
    public function del_obj()

    {

        $id = input('param.id');

        $cate = new ProductFileModel();

        $flag = $cate->delObj($id);

        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);

    }
    //修改状态
    public function obj_state()

    {

        extract(input());

        $cate = new ProductFileModel();

        $flag = $cate->objState($id, $num);

        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);

    }

    //批量删除
    public function batchDelObj(){

        extract(input());

        if(empty($ids)){

            return json(['code'=>100,'msg'=>'请选择要删除的记录！']);

        }

        $cate = new ProductFileModel();

        $flag = $cate->batchDel($ids);

        return json(['code' => $flag['code'], 'msg' => $flag['msg']]);

    }
    //批量禁用
    public function forbiddenObj(){

        extract(input());

        $list = [];

        if($ids){

            $ids = explode(',',$ids);

            for($i=0;$i<count($ids);$i++){

                $param = [

                    'id'=>$ids[$i],

                    'status'=>2

                ];

                $list[] = $param;

            }

        }

        $cate = new ProductFileModel();

        $flag = $cate->forbiddenObj($list);

        return json(['code' => $flag['code'], 'msg' => $flag['msg']]);

    }

    //批量启用
    public function usingObj(){

        extract(input());

        $list = [];

        if($ids){

            $ids = explode(',',$ids);

            for($i=0;$i<count($ids);$i++){

                $param = [

                    'id'=>$ids[$i],

                    'status'=>1

                ];

                $list[] = $param;

            }

        }

        $cate = new ProductFileModel();

        $flag = $cate->usingObj($list);

        return json(['code' => $flag['code'], 'msg' => $flag['msg']]);

    }

    //修改排序
    public function editField(){

        extract(input());

        $res = Db::name($table)->where(['id' => $id ])->setField($field , $value);

        if($res){

            writelog('更新字段成功',200);

            return json(['code' => 200,'data' => '', 'msg' => '更新字段成功']);

        }else{

            writelog('更新字段失败',100);

            return json(['code' => 100,'data' => '', 'msg' => '更新字段失败']);

        }

    }
}